﻿$(function(){//延迟加载
	var jfId='';
	var uprows=null;
	var deleterows=null;
	var newrows=null;
	var done=0;
	var jxId='';
	$('#b1').linkbutton({
		iconCls:'icon-add',
		plain:true,
		onClick:function(){
			append();
		}
	});
	$('#b2').linkbutton({
		iconCls:'icon-edit',
		plain:true,
		onClick:function(){
		removeit();
		}
	});
	$('#b3').linkbutton({
		iconCls:'icon-save',
		plain:true,
		onClick:function(){
		accept();
		}
	});
	$('#b4').linkbutton({
		iconCls:'icon-remove',
		plain:true,
		onClick:function(){
		reject();
		}
	});
	
	$('#jflist').datagrid({
		width:'auto',
		height:'auto',
		toolbar:'#tb2',
		singleSelect: true,
		url:'showjflist',
		queryParams:{
			jxId:'',
		},
		method:'get',
		pagination:true,
		rownumbers:true,
		fit:true,
		onDblClickRow: onClickRow,
		columns:[[				
			{field:'jfname',
			title:'机房名称',
			width:'15%',editor:{
	        	  type:'textbox',
	        		 options:{
	        			 editable:true,
	        			 required:true 
	        		 }
	          }},
			{field:'djxname',
			title:'大局向',
			width:'15%',
			editor:{
	        	  type:'textbox',
	        		 options:{
	        			 editable:false,
	        			 required:true 
	        		 }
	          }},
			{field:'jxname',
			title:'局向',
			width:'15%',editor:{
	        	  type:'textbox',
	        		 options:{
	        			 editable:false,
	        			 required:true 
	        		 }
	          }},
			{field:'jfloc',
			title:'位置描述',
			width:'20%',editor:{
	        	  type:'textbox',
	        		 options:{
	        			 editable:true
	        		 }
	          }},
			{field:'jingdu',
			title:'经度',
			width:'15%',editor:{
	        	  type:'textbox',
	        		 options:{
	        			 editable:true,
	        		 }
	          }},
			{field:'weidu',
			title:'纬度',
			width:'15%',editor:{
	        	  type:'textbox',
	        		 options:{
	        			 editable:true
	        		 }
	          }}
			]],
			onClickRow:function(index,row){
				jfId=row.jfId;
				$('#jfdvlist').datagrid('reload',{
					dvbelong:'',
					jfId:jfId
				});
			},
			onLoadSuccess:function(data){
				var pager = $('#jflist').datagrid('getPager');	// get the pager of datagrid
				pager.pagination({
					buttons:[{
						iconCls:'icon-ok',
						text:'保存修改',
						handler:function(){
							if(done==0){
								alert('请先确定修改');
								return;
							}
							alert(uprows.length);
							alert(newrows.length);
							alert(deleterows.length);
							var listupdate=uprows.length==0?'':JSON.stringify(uprows);
							var listnew=newrows.length==0?'':JSON.stringify(newrows);
							var listdelete=deleterows.length==0?'':JSON.stringify(deleterows);
							$.ajax({
								url:'updatejfrows',
								tpye:'POST',
								data:{
									listupdate:listupdate,
									listnew:listnew,
									listdelete:listdelete
								},
								success:function(data){
									alert(data.msg);
									$('#jflist').datagrid('reload',{
										jxId:jxId
									});
								}
							});
						}
					},{
						iconCls:'icon-edit',
						text:'所属局向修改',
						handler:function(){
							var jfrows=$('#jflist').datagrid('getSelected');
							if(jfrows==null){
								alert('未选择机房');
							}else
							parent.addTab('修改所属局向','toeditjxofjf?jfId='+jfrows.jfId);
						}
					},{
						iconCls:'icon-search',
						text:'机房地理位置',
						handler:function(){
							var jfrows=$('#jflist').datagrid('getSelected');
							if(jfrows==null){
								alert('未选择机房');
							}else
							parent.addTab('机房地理位置','tobaidujfmap?jfId='+jfrows.jfId);
						}
					}]
				});					
			}
	});
	
	$('#dajuxiang').combobox({
		onChange:function(newValue,oldValue){
			if(newValue=='0'){				
				$('#juxiang').combobox({					
					disabled:true
				});
				$('#juxiang').empty();
			}else{
				$('#juxiang').empty();
				$.ajax({
					url:"showjx?djxId="+newValue,
					success:function(data){
						$('#juxiang').append('<option value="0">请选择</option>');
						for(var i=0;i<data.length;i++){
							$('#juxiang').append('<option value="'+data[i].jxId+'">'+data[i].jxname+'</option>');
						}	
						$('#juxiang').combobox();
					}
				});
//				$('#juxiang').combobox({
//					value:'请选择',
//					disabled:false,
//					valueField: 'jxId',    
//			        textField: 'jxname',
//					url:"showjx?djxId="+newValue,
//				});
			}
		}
	});
	
	$('#juxiang').combobox({
		onChange:function(newValue,oldValue){
			jxId=newValue;
			$('#jflist').datagrid('reload',{
				jxId:newValue
			});
		}
	});
	
	
	
	var editIndex = undefined;
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#jflist').datagrid('validateRow', editIndex)){
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	}
	
	function onClickRow(index){
		done=0;
		if (editIndex != index){
			if (endEditing()){
				$('#jflist').datagrid('selectRow', index)
						.datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#jflist').datagrid('selectRow', editIndex);
			}
		}
	}
	function append(){
		if (endEditing()){
			var jxId=$('#juxiang').combobox('getValue');
			if(jxId=='0'){
				alert('请选择局向');
				return '0';
			}
			var jxname=$('#juxiang').combobox('getText');
			var djxId=$('#dajuxiang').combobox('getValue');
			var djxname=$('#dajuxiang').combobox('getText');
			$('#jflist').datagrid('appendRow',
				{
				jfId:'',
				jxId:jxId,
				jxname:jxname,
				djxId:djxId,
				djxname:djxname
			});
			editIndex = $('#jflist').datagrid('getRows').length-1;
			$('#jflist').datagrid('selectRow', editIndex).datagrid('beginEdit', editIndex);				
		}
		done=0;
	}
	function removeit(){
		if (editIndex == undefined){return}
		$('#jflist').datagrid('cancelEdit', editIndex)
				.datagrid('deleteRow', editIndex);
		editIndex = undefined;
	}
	function accept(){
		if(endEditing()){
			done=1;
			uprows = $('#jflist').datagrid('getChanges','updated');
			newrows = $('#jflist').datagrid('getChanges','inserted');
			deleterows=$('#jflist').datagrid('getChanges','deleted');
			$('#jflist').datagrid('acceptChanges');
			editIndex = undefined;
		}
		return;
		
		var crow = $('#jflist').datagrid('getChanges','updated');
		alert(crow.length);	
		if(crow.length==0){
			crow=$('#jflist').datagrid('getChanges','inserted');
		}
		$('#jflist').datagrid('acceptChanges');
		alert(crow.length);
		return 
		if(crow==0){
			alert('未修改');
			editIndex = undefined;
			acceptindex='1';
			return ;//没有修改
		}
		var jfr=$('#jflist').datagrid('getRows')[editIndex];
		var jxId=jfr.jxId,jxname=jfr.jxname,djxId=jfr.djxId,djxname=jfr.djxname,jfloc=jfr.jfloc,
		jingdu=jfr.jingdu,weidu=jfr.weidu,jfname=jfr.jfname,jfId=jfr.jfId;
		url='updatejfrow';
//		alert(jfname);
//		alert(jxId);
//		return '0';
		$.ajax({
			url:url,
			data:{
				jxId:jxId,
				jxname:jxname,
				djxId:djxId,
				djxname:djxname,
				jingdu:jingdu,
				weidu:weidu,
				jfloc:jfloc,
				jfname:jfname,
				jfId:jfId
			},
			type:'POST',
			success:function(data){
				alert(data.msg);
				status=data.status;
				if(status=='1'){
					editIndex = undefined;
					acceptindex='1';			
				}else{
					acceptindex='0';
				}
				$('#jflist').datagrid('reload',{
					jxId:jxId
				});
			}
		});
	}
	function reject(){
		$('#jflist').datagrid('rejectChanges');
		acceptindex='1';
		editIndex = undefined;
	}
	function updatejfrow(queryparam,url){
		var status='';
		$.ajax({
			url:url,
			data:queryparam,
			type:'POST',
			success:function(data){
				alert(data.msg);
				status=data.status;
			}
		});
		return status;
	}
	
	$('#jfdvlist').datagrid({
		url:'showdvlistofjf',
		queryParams:{
			jfId:'',
			dvbelong:''
		},
		pagination:true,
		rownumbers:true,
		height:'320px',
		toolbar:'#dvbelongsectool',
		columns:[[{
			field:'dvname',
			title:'设备名称',
			width:'25%'
		},{
			field:'dvip',
			title:'设备ip',
			width:'15%'
		},{
			field:'dvbelong',
			title:'设备类型',
			width:'15%'
		},{
			field:'dvmodel',
			title:'设备型号',
			width:'15%'
		},{
			field:'jfname',
			title:'设备机房',
			width:'25%'
		}]]
	});
	var pager = $('#jfdvlist').datagrid('getPager');	// get the pager of datagrid
	pager.pagination({
		buttons:[{
			iconCls:'icon-edit',
			text:'设备详细信息',
			handler:function(){
				var row=$('#jfdvlist').datagrid('getSelected');
			}
		},{
			iconCls:'icon-search',
			text:'设备详细拓扑',
			handler:function(){
				var row=$('#jfdvlist').datagrid('getSelected');
				if(row!=undefined){
					parent.addTab(row.dvname,'tojxtp?dvIds='+row.dvbelong+','+row.dvId);
				}
			}
		}]
	});
	
	$('#dvbelongsec').combobox({
		onChange:function(newValue,oltValue){
			if(jfId!='')
			$('#jfdvlist').datagrid('reload',{
				dvbelong:newValue,
				jfId:jfId
			});
		}
	});
	//function updatejfrow
});